// pages/me/addAddress.js
const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['福建省', '厦门市', '海沧区'],
    customItem: '全部'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 表单提交
  formSubmit: function (e) {
    console.log(e);
    var that = this;
    if (e.detail.value.name == '') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '收货人名不能为空',
        cancelColor: '#fc5871',
        confirmColor: '#fc5871',
      })
    }
    else if (e.detail.value.phone == '') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '联系电话不能为空',
        cancelColor: '#fc5871',
        confirmColor: '#fc5871',
      })
    }
    else if (e.detail.value.address == '') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '详细地址不能为空',
        cancelColor: '#fc5871',
        confirmColor: '#fc5871',
      })
    }
    else {
      wx.request({
        url: app.globalData.serverUrl + 'addReceiveAddress',
        method: 'GET',
        data: {
          openid: app.globalData.openid,
          name: e.detail.value.name,
          phone: e.detail.value.phone,
          address: e.detail.value.address,
          province: that.data.region[0],
          city: that.data.region[1],
          area: that.data.region[2]
        },
        success: function (e) {
          // console.log(e);
          if (e.data.status == 1) {
            wx.showToast({
              title: '添加成功',
              icon: 'success',
              duration: 2000,
              success: function (res) {
                // console.log(res);
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000);
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '添加失败'
            })
          }
        }
      })
      // wx.showModal({
      //   content: '收货地址添加成功！',
      //   showCancel: false,
      //   cancelColor: '#fc5871',
      //   confirmColor: '#fc5871',
      //   success:function(e){
      //     wx.navigateBack({
      //       delta: 1
      //     })
      //   }
      // })
    }

  },
  // 省市区选择
  bindRegionChange: function (e) {
    // console.log(e);
    this.setData({
      region: e.detail.value
    });
  }
})